/**
 * version 1.0
 * 常用工具类封装
 * 
 */
; +
function() {
	function isUndefined(arg) {
		return arg === undefined ? true : false;
	}
	//事件工具类
	window.eventUtils = {
		add: function(ele, eventName, callback, isCapture) { //函数懒加载
			if(ele.addEventListener) {
				ele.addEventListener(eventName, callback, isCapture);
				this.add = function(ele, eventName, callback, isCapture) {
					ele.addEventListener(eventName, callback, isCapture);
					return this;
				}
				return this;
			} else { //兼容ie
				ele.attachEvent('on' + eventName, callback);
				this.add = function(ele, eventName, callback) {
					ele.attachEvent('on' + eventName, callback);
					return this;
				}
				return this;
			}
		},
		remove: function(ele, eventName, callback) {
			if(ele.removeEventListener) {
				ele.removeEventListener(eventName, callback);
				this.remove = function(ele, eventName, callback) {
					ele.removeEventListener(eventName, callback);
					return this;
				}
				return this;
			} else {
				ele.detachEvent('on' + eventName, callback);
				this.remove = function(ele, eventName, callback) {
					ele.detachEvent('on' + eventName, callback);
					return this;
				}
				return this;
			}
		}
	}
	//随机工具类
	window.randomUtils = {
		int: function(min, max) {
			if(isUndefined(max)) {
				var max = min;
				var min = 0;
			}
			return Math.round(Math.random() * (max - min)) + min;
		},
		color:function(){ //返回一个rgb格式的随机颜色
			return "rgb("+this.int(255)+","+this.int(255)+","+this.int(255)+")";
		}
	}
	//类JQuery选择器
	window.$ = function(selector) {
		if(selector === undefined || typeof selector != "string") return;
		var match = null;
		//#id
		if(match = selector.match(/^#(.+)/)) {

			return document.getElementById(match[1]);
		}
		//.class
		if(match = selector.match(/^\.(.+)/)) {
			return document.getElementsByClassName(match[1])[0];
		}
		//div
		if(match = selector.match(/^([a-zA-Z]+[0-9]?)$/)) {
			return document.getElementsByTagName(match[1])[0];
		}
		//div[index= 8]
		if(match = selector.match(/^([a-zA-Z]+[0-9]?)\s*\[\s*([^\s]+)\=\s*([^\s]+)\s*\]$/)) {
			var eles = $$(match[1]);
			for(var i = 0; i < eles.length; i++) {
				if(eles[i].getAttribute(match[2]) == match[3]) {
					return eles[i];
				}
			}
			return null;
		}
	}
	window.$$ = function(selector) { //选择器
		if(selector === undefined || typeof selector != "string") return;
		var match = null;
		//#id
		if(match = selector.match(/^#(.+)/)) {
			return document.getElementById(match[1]);
		}
		//.class
		if(match = selector.match(/^\.(.+)/)) {
			return Array.prototype.slice.call(document.getElementsByClassName(match[1]));
		}
		//div
		if(match = selector.match(/^([a-zA-Z]+[0-9]?)$/)) {
			return Array.prototype.slice.call(document.getElementsByTagName(match[1]));
		}
		//div[index= 8]
		if(match = selector.match(/^([a-zA-Z]+[0-9]?)\s*\[\s*([^\s]+)\=\s*([^\s]+)\s*\]$/)) {
			var eles = $$(match[1]);
			var res = new Array();
			for(var i = 0; i < eles.length; i++) {
				if(eles[i].getAttribute(match[2]) == match[3]) {
					res.push(eles[i]);
				}
			}
			return res;
		}
	}

}();